package GUI;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import GUI.manager.VentanaFactory;
import GUI.manager.VentanaManager;
import core.model.Sugerencia;

public class panelAdministradorSugerencias extends javax.swing.JPanel implements
		ActionListener, ItemListener, MouseListener {
	
	private VentanaManager ventanaManager=VentanaFactory.getInstance();
	private java.util.List<Sugerencia> sugerencias=ventanaManager.getSugerenciasByCode(0);
	private DefaultTableModel modelo=new DefaultTableModel(convertirArray(sugerencias), new Object[]{"Codigo","Nick","Asunto","Fecha"});
	private Point punto=new Point();
	public static int codigo_sugerencia;
	public panelAdministradorSugerencias() {
		initComponents();
	}

	private void initComponents() {

		 panelCentro2 = new javax.swing.JPanel();
	        jScrollPane1 = new javax.swing.JScrollPane();
	        jTable1 = new javax.swing.JTable();
	        eSubcategoriaProducto1 = new javax.swing.JLabel();
	        jScrollPane2 = new javax.swing.JScrollPane();
	        taDescripcion1 = new javax.swing.JTextArea();
	        bAgregar = new javax.swing.JButton();
	        bEliminar = new javax.swing.JButton();
	        cTipo = new javax.swing.JComboBox();
	        eTipo = new javax.swing.JLabel();
	        botonSalir = new javax.swing.JButton();
	        bTitulo = new javax.swing.JButton();
	        rbSugerencias = new javax.swing.JRadioButton();
	        rbGestion = new javax.swing.JRadioButton();
	        bAtras = new javax.swing.JButton();

		setBackground(new java.awt.Color(255, 255, 255));

		panelCentro2.setBackground(new java.awt.Color(255, 255, 255));
		jTable1.setEnabled(true);
		jTable1.setModel(modelo);
		jTable1.addMouseListener(this);
		jTable1.addMouseListener(new MouseAdapter() 
		   {
		      public void mouseClicked(MouseEvent e) 
		      {
		         int fila = jTable1.rowAtPoint(e.getPoint());
		         int valor=Integer.parseInt(jTable1.getValueAt(fila, 0).toString());
		         Sugerencia sug=ventanaManager.getSugerencia(valor);
		         codigo_sugerencia=valor;
		         
		         taDescripcion1.setText(sug.getDescripcion());
		         
		         
		        
			         
			     int columna = jTable1.columnAtPoint(e.getPoint());
		         if ((fila > -1) && (columna > -1)){
		        	 
		         }
		           
		      }
		      });;
		jScrollPane1.setViewportView(jTable1);

	

		eSubcategoriaProducto1.setBackground(new java.awt.Color(255, 255, 255));
		eSubcategoriaProducto1.setFont(new java.awt.Font("Lucida Handwriting",
				1, 13)); // NOI18N
		eSubcategoriaProducto1.setForeground(new java.awt.Color(102, 102, 102));
		eSubcategoriaProducto1.setText("DESCRIPCION");

		

		taDescripcion1.setColumns(20);
		taDescripcion1.setRows(5);
		taDescripcion1.setFont(new java.awt.Font("Calibri", 0, 13));
		jScrollPane2.setViewportView(taDescripcion1);

		bAgregar.setBackground(new java.awt.Color(255, 255, 255));
		bAgregar.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bAgregar.setForeground(new java.awt.Color(243, 15, 120));
		bAgregar.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonAgregar.png")); // NOI18N
		bAgregar.setBorder(null);
		bAgregar.addActionListener(this);

		

		bEliminar.setBackground(new java.awt.Color(255, 255, 255));
		bEliminar.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bEliminar.setForeground(new java.awt.Color(243, 15, 120));
		bEliminar.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonEliminar.png")); // NOI18N
		bEliminar.setBorder(null); // NOI18N
		bEliminar.setBorder(null);
		bEliminar.addActionListener(this);

		cTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Productos", "Otras" }));
		cTipo.addItemListener(this);

		eTipo.setBackground(new java.awt.Color(255, 255, 255));
		eTipo.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eTipo.setForeground(new java.awt.Color(243, 15, 120));
		eTipo.setText("TIPO DE SUGERENCIA");

		org.jdesktop.layout.GroupLayout panelCentro2Layout = new org.jdesktop.layout.GroupLayout(panelCentro2);
        panelCentro2.setLayout(panelCentro2Layout);
        panelCentro2Layout.setHorizontalGroup(
            panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCentro2Layout.createSequentialGroup()
                .add(12, 12, 12)
                .add(eSubcategoriaProducto1)
                .addContainerGap(566, Short.MAX_VALUE))
            .add(panelCentro2Layout.createSequentialGroup()
                .add(21, 21, 21)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelCentro2Layout.createSequentialGroup()
                        .add(97, 97, 97)
                        .add(bAgregar)
                        .add(84, 84, 84)
                        .add(bEliminar)
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(panelCentro2Layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 634, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 22, Short.MAX_VALUE))))
            .add(panelCentro2Layout.createSequentialGroup()
                .add(eTipo)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cTipo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 314, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 0, Short.MAX_VALUE))
            .add(jScrollPane1)
        );
        panelCentro2Layout.setVerticalGroup(
            panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCentro2Layout.createSequentialGroup()
                .add(4, 4, 4)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cTipo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(eTipo))
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(eSubcategoriaProducto1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 183, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(bAgregar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bEliminar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(49, Short.MAX_VALUE))
        );

		botonSalir.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/menu-salir.png")); // NOI18N
		botonSalir.setBorderPainted(false);
		botonSalir.setBackground(new java.awt.Color(255, 255, 255));
		botonSalir.addActionListener(this);

		bTitulo.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/encabezadoMain.png"));
		bTitulo.setBorder(null);
		bTitulo.setBorderPainted(false);
		bTitulo.setBackground(new java.awt.Color(255, 255, 255));
		bTitulo.addActionListener(this);
		
		rbSugerencias.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		rbSugerencias.setForeground(new java.awt.Color(243, 15, 120));
		rbSugerencias.setText("SUGERENCIAS");
		rbSugerencias.setSelected(true);
		rbSugerencias.setEnabled(false);
		rbSugerencias.setBackground(new java.awt.Color(255, 255, 255));

		rbGestion.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		rbGestion.setForeground(new java.awt.Color(243, 15, 120));
		rbGestion.setText("GESTION");
		rbGestion.addItemListener(this);
		rbGestion.setBackground(new java.awt.Color(255, 255, 255));

		bAtras.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/boton_atras.png")); // NOI18N
		bAtras.setBorder(null);
		bAtras.setBackground(new java.awt.Color(255, 255, 255));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(bAtras)
								.add(layout
										.createSequentialGroup()
										.addContainerGap()
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.LEADING)
												.add(rbGestion)
												.add(rbSugerencias))))
						.add(18, 18, 18)
						.add(panelCentro2,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE))
				.add(layout
						.createSequentialGroup()
						.add(96, 96, 96)
						.add(bTitulo)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).add(botonSalir)));
		layout.setVerticalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(6, 6, 6)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(bTitulo,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										104,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(botonSalir))
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createSequentialGroup()
										.add(rbSugerencias)
										.add(18, 18, 18)
										.add(rbGestion)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE).add(bAtras)
										.add(14, 14, 14))
								.add(layout
										.createSequentialGroup()
										.add(panelCentro2,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)))));
	}
	
	public String[][] convertirArray(java.util.List<Sugerencia>sugerencias)
	{
		String [][] datos=new String [sugerencias.size()][4];
		for(int i=0;i<sugerencias.size();i++)
		{
			datos[i][0]=String.valueOf(sugerencias.get(i).getCodigo_sugerencia());
			datos[i][1]=sugerencias.get(i).getNick();
			datos[i][2]=sugerencias.get(i).getAsunto();
			//datos[i][3]=sugerencias.get(i).getDescripcion();
			datos[i][3]=String.valueOf(sugerencias.get(i).getFecha());

			
		}
		return datos;
	}

	 private javax.swing.JButton bAtras;
	    private javax.swing.JButton bAgregar;
	    private javax.swing.JButton bEliminar;
	    private javax.swing.JButton bTitulo;
	    private javax.swing.JButton botonSalir;
	    private javax.swing.JComboBox cTipo;
	    private javax.swing.JLabel eSubcategoriaProducto1;
	    private javax.swing.JLabel eTipo;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JScrollPane jScrollPane2;
	    private javax.swing.JTable jTable1;
	    private javax.swing.JPanel panelCentro2;
	    private javax.swing.JRadioButton rbGestion;
	    private javax.swing.JRadioButton rbSugerencias;
	    private javax.swing.JTextArea taDescripcion1;
	private JFrame actual;
	private JPanel estePanel = this;

	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == botonSalir) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSalir(27));
			actual.setPreferredSize(new Dimension(610, 330));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bTitulo) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelAdministradorPrincipal());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bEliminar) {
			
			ventanaManager.deleteSugerencia(codigo_sugerencia);
			modelo=new DefaultTableModel(convertirArray(sugerencias), new Object[]{"Codigo","Autor","Titulo","Fecha"});
			jTable1.setModel(modelo);
			jTable1.updateUI();
			
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelAdministradorSugerencias());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);
		}

		if (e.getSource() == bAgregar) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelAdministradorGestionProductos());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}


		}

	


	public void itemStateChanged(ItemEvent e) {
		if (e.getSource() == cTipo) {

			if (cTipo.getSelectedItem().equals("Otras")) {
				actual = (JFrame) UtilidadesGUI
						.getContenedorPrincipal(estePanel);
				actual.getContentPane().remove(0);
				actual.getContentPane().add(
						new panelAdministradorSugerenciasOtras());
				actual.setPreferredSize(new Dimension(850, 630));
				actual.pack();
				actual.repaint();
				UtilidadesGUI.CentrarPantalla(actual);

			}

		}

		if (e.getSource() == rbGestion) {

			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(
					new panelAdministradorGestionProductos());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);
		}
		

	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
